package com.zsc.wxm.ysh.anjo.house.controller;


import com.zsc.wxm.ysh.anjo.house.service.EstateService;
import com.zsc.wxm.ysh.anjo.result.PageResult;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.model.es.dto.CrawlerDataDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.EstateDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.qo.EstateQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author playmaker
 * @since 2021-01-04
 */
@RestController
@RequestMapping("/house/estate")
public class EstateController {

    @Resource
    private EstateService estateService;

    @PostMapping("/add")
    @ApiOperation("增加楼盘信息")
    public Result add(@Validated @RequestBody EstateDTO params) {
        estateService.addEstate(params);
        return Result.ok("增加成功");
    }

    @PutMapping("/update")
    @ApiOperation("修改楼盘信息")
    public Result update(@Validated @RequestBody EstateDTO params) {
        estateService.updateEstate(params);
        return Result.ok("修改成功");
    }

    @PostMapping("/list-page")
    @ApiOperation("分页查询")
    public PageResult listPage(@Validated @RequestBody EstateQuery params) {
        return estateService.listPage(params);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public Result delete(@PathVariable Long id) {
        estateService.deleteEstate(id);
        return Result.ok("删除成功");
    }

    @PostMapping("/add-data")
    @ApiOperation("批量导入楼盘数据")
    public Result addData(@RequestBody CrawlerDataDTO params) {
        estateService.batchAddEstateDataByFile(params);
        return Result.ok("导入成功");
    }

    @GetMapping("/get-one-by-id/{id}")
    @ApiOperation("根据id获取数据")
    public Result getOneById(@PathVariable("id") Long id) {
        Map<String, Object> data = new HashMap<>();
        data.put("data", estateService.getOneById(id));
        return Result.ok("获取成功", data);
    }
}

